package me.shurufa.controller;

import android.support.v7.widget.RecyclerView;

import java.util.List;

import me.shurufa.bean.Book;
import me.shurufa.utils.Utils;

/**
 * Author : andy
 * Date   : 16/4/10 13:06
 * Email  : andyxialm@gmail.com
 * Github : github.com/andyxialm
 * Description : 书摘书评的更新计数 Controller
 */
public class CounterController {
    private RecyclerView.Adapter mAdapter;
    private List<Book> mDataList;
    private CounterType mCounterTyle;
    private int mBookId;

    public enum CounterType {
        DIGEST, BOOKCOMMENT;
    }
    public CounterController(RecyclerView.Adapter adapter, List<Book> dataList, int bookId, CounterType type) {
        mAdapter = adapter;
        mDataList = dataList;
        mBookId = bookId;
        mCounterTyle = type;
    }

    public void handleAddCounter() {
        try {
            if (Utils.isNullForList(mDataList)) {
                return;
            }

            if (mBookId == 0) {
                return;
            }

            // 遍历查询所有书，匹配id
            for (Book b : mDataList) {
                if (mBookId == b.id) {
                    // 已查询到，判断是书摘 还是 书评, 做 +1 操作
                    if (CounterType.DIGEST == mCounterTyle) {
                        int digestNum = b.excerpt_num;
                        digestNum += 1;
                        b.excerpt_num = digestNum;
                        break;
                    }

                    if (CounterType.BOOKCOMMENT == mCounterTyle) {
                        int commentNum = Integer.valueOf(b.comment_num);
                        commentNum += 1;
                        b.comment_num = commentNum;
                        break;
                    }
                }
            }
            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
        }
    }

    public void handleDelCounter() {
        try {
            if (Utils.isNullForList(mDataList)) {
                return;
            }

            if (mBookId == 0) {
                return;
            }

            // 遍历查询所有书，匹配id
            for (Book b : mDataList) {
                if (mBookId == b.id) {
                    // 已查询到，判断是书摘 还是 书评, 做 +1 操作
                    if (CounterType.DIGEST == mCounterTyle) {
                        int digestNum = b.excerpt_num;
                        digestNum -= 1;
                        if (digestNum <= 0) {
                            digestNum = 0;
                        }
                        b.excerpt_num = digestNum;
                        break;
                    }

                    if (CounterType.BOOKCOMMENT == mCounterTyle) {
                        int commentNum = b.comment_num;
                        commentNum -= 1;
                        if (commentNum <= 0) {
                            commentNum = 0;
                        }
                        b.comment_num = commentNum;
                        break;
                    }
                }
            }
            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
        }
    }
}
